﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using FlickrNet;
using SharePointContentGenerator.ContentGeneratorLogic.Core;
using SharePointContentGenerator.ContentGeneratorLogic.DataObjects;

namespace SharePointContentGenerator.ContentGeneratorLogic.ContentGenerators
{
    public class PictureGenerator : ContentGeneratorNotification
    {
        private const string flickrAPIKey = "8bdd6a95e0481416609ac64bee6785b9";

        #region Properties

        public bool Downloading { get; private set; }
        public int TotalItems { get; private set; }

        #endregion Properties

        #region Methods

        private Stream DownloadData(string url)
        {
            Stream result = null;

            WebRequest req = WebRequest.Create(url);
            WebResponse response = req.GetResponse();
            result = response.GetResponseStream();

            return result;
        }

        private Image GetImageFromUrl(string url)
        {
            Image result = null;

            Stream stream = DownloadData(url);
            result = Image.FromStream(stream);
            stream.Close();

            return result;
        }

        public List<Picture> GetPhotosFromFlickr(string searchTags, int numPictures)
        {
            List<Picture> result = new List<Picture>();
            Downloading = true;
            try
            {
                Flickr F = new Flickr(flickrAPIKey);

                PhotoSearchOptions searchOptions = new PhotoSearchOptions();
                searchOptions.Tags = searchTags;

                // Need to work out number of pages and number per page
                // Flickr limit is 500 per page
                double numPages = (numPictures / 500) + 1;
                int iNumPages = (int)Math.Round(numPages, 0);
                int numPerPage = numPictures / iNumPages;

                searchOptions.PerPage = numPerPage;
                List<Photo> allPhotos = new List<Photo>();
                for (int i = 0; i < iNumPages; i++)
                {
                    searchOptions.Page = i + 1;
                    PhotoCollection tempPhotos = F.PhotosSearch(searchOptions);
                    allPhotos.AddRange(tempPhotos.ToList());
                }

                TotalItems = allPhotos.Count;

                DoContentGenerationItemsStarted(TotalItems);
                bool cancel = false;
                for (int i = 0; i < TotalItems; i++)
                {
                    Photo current = allPhotos[i];
                    cancel = DoContentGenerationItemChanging(current.Title, i, TotalItems, ContentGeneratorAction.Downloading);
                    if (cancel)
                        break;

                    result.Add(new Picture()
                        {
                            Title = current.Title,
                            Thumbnail = GetImageFromUrl(current.ThumbnailUrl),
                            ThumbnailFileName = Path.GetFileName(current.ThumbnailUrl),
                            ThumbnailUrl = current.ThumbnailUrl,
                            Image = GetImageFromUrl(current.MediumUrl),
                            ImageFileName = Path.GetFileName(current.MediumUrl),
                            ImageUrl = current.MediumUrl
                        });
                }

                DoContentGenerationItemsFinished();
            }
            finally
            {
                Downloading = false;
            }

            return result;
        }

        #endregion Methods
    }
}
